package com.uottawa.clickerapp.dao.impl;

/**
 * @author Liam
 */
public class DBHelper {
/*
	public static boolean store(QuestionSession qs){
		
    	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    	Entity qse = new Entity("QuestionSession");

    	qse.setProperty("keyword", qs.getKeyword());
    	qse.setProperty("password", qs.getPassword());
    	qse.setProperty("time", new Date());
    	
    	datastore.put(qse);
    	
    	qs.setID(qse.getKey().getId());
    	
		return true;
	}
	
	public static boolean store(Question q){
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    	Entity qe = new Entity("Question");

    	qe.setProperty("questionSessionID", q.getQuestionSessionID());
    	qe.setProperty("question", q.getQuestionText());
    	qe.setProperty("multipleSubmit", q.canMultipleSubmit());
      	qe.setProperty("enabled", false);
    	qe.setProperty("time", new Date());
    	
    	datastore.put(qe);
    	
    	q.setID(qe.getKey().getId());
    	
		return true;
	}
	
	public static boolean store(Answer a){
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    	Entity ae = new Entity("Answer");

    	ae.setProperty("questionID", a.getQuestion().getID());
      	ae.setProperty("answer", a.getAnswerText());
    	
    	datastore.put(ae);
    	
    	a.setID(ae.getKey().getId());
    	
		return true;		
	}

	public static void fetch(QuestionSession qs){
    	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    	KeyFactory.Builder keyBuilder = new KeyFactory.Builder("QuestionSession", qs.getID());
    	Key k = keyBuilder.getKey();
    	Entity e;
		try {
			e = datastore.get(k);
			qs.setKeyword(e.getProperty("keyword").toString());
			qs.setPassword(e.getProperty("password").toString());
		} catch (Exception ex){};
	}
	
	public static void fetch(Question q){
    	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    	KeyFactory.Builder keyBuilder = new KeyFactory.Builder("Question", q.getID());
    	Key k = keyBuilder.getKey();
    	Entity e;
		try {
			e = datastore.get(k);
			q.setEnabled(Boolean.valueOf(e.getProperty("enabled").toString()));
			q.setQuestionSessionID(Long.valueOf(e.getProperty("questionSessionID").toString()));
			q.setQuestionText(e.getProperty("question").toString());
			q.setMultipleSubmit(Boolean.valueOf(e.getProperty("multipleSubmit").toString()));
		} catch (Exception ex){};	
	}
	
	public static void fetch(Answer a){
    	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    	KeyFactory.Builder keyBuilder = new KeyFactory.Builder("Answer", a.getID());
    	Key k = keyBuilder.getKey();
    	Entity e;
		try {
			e = datastore.get(k);
			a.setAnswerText(e.getProperty("answer").toString());
		} catch (Exception ex){};	
	}
	
	public static void delete(QuestionSession qs){
    	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    	KeyFactory.Builder keyBuilder = new KeyFactory.Builder("QuestionSession", qs.getID());
    	Key k = keyBuilder.getKey();
		try {
			datastore.delete(k);
		} catch (Exception e){};
	}
	
	public static void delete(Question q){
    	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    	KeyFactory.Builder keyBuilder = new KeyFactory.Builder("Question", q.getID());
    	Key k = keyBuilder.getKey();
		try {
			datastore.delete(k);
		} catch (Exception e){};
	}
	
	public static void delete(Answer a){
    	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    	KeyFactory.Builder keyBuilder = new KeyFactory.Builder("Answer", a.getID());
    	Key k = keyBuilder.getKey();
		try {
			datastore.delete(k);
		} catch (Exception e){};
	}
	
	public static boolean submitPoll(long questionID, long answerID, long submittedBy, boolean multipleSubmit){
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		
		Query q = new Query("Poll");
    	q.addFilter("questionID", Query.FilterOperator.EQUAL, questionID);
    	q.addFilter("submittedBy", Query.FilterOperator.EQUAL, submittedBy);

    	// PreparedQuery contains the methods for fetching query results
    	// from the data store
    	PreparedQuery pq = datastore.prepare(q);
    	
    	if (pq.countEntities(FetchOptions.Builder.withOffset(0)) == 0 ){

	    	Entity message = new Entity("Poll");
	
	    	message.setProperty("questionID", answerID);
	    	message.setProperty("answerID", answerID);
	    	message.setProperty("submittedBy", submittedBy);
	    	message.setProperty("time", new Date());
	      	
	    	
	    	datastore.put(message);
	    	
			return true;
    	} else if( multipleSubmit == true){
    		Entity t = pq.asSingleEntity();
	    	t.setProperty("answerID", answerID);
    		datastore.put(t);
    		return true;
    	}else return false;
	}
	
	public static void getPolls(Answer a){
    	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    	
    	// The Query interface assembles a query
    	Query q = new Query("Poll");
    	q.addFilter("answerID", Query.FilterOperator.EQUAL, a.getID());

    	// PreparedQuery contains the methods for fetching query results
    	// from the data store
    	PreparedQuery pq = datastore.prepare(q);
    	
    	a.setPolls(pq.countEntities(FetchOptions.Builder.withOffset(0)));
	}
	
	public static PollResults getPolls(long questionID){
    	
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    	PollResults results = new PollResults();
    	KeyFactory.Builder keyBuilder = new KeyFactory.Builder("Question", questionID);
    	Key s = keyBuilder.getKey();
    	Entity temp;
		try {
			temp = datastore.get(s);
			Question ques = new Question();
			ques.setQuestionText(temp.getProperty("question").toString());
			results.setQuestion(ques);
			
			Query q = new Query("Answer");
	    	q.addFilter("questionID", Query.FilterOperator.EQUAL, questionID);

	    	// PreparedQuery contains the methods for fetching query results
	    	// from the data store
	    	PreparedQuery pq = datastore.prepare(q);

	    	for (Entity result : pq.asIterable()) {
				  Answer t = new Answer();
				  t.setID(result.getKey().getId());
				  t.setAnswerText(result.getProperty("answer").toString());
				  
				  Query qu = new Query("Poll");
				  q.addFilter("answerID", Query.FilterOperator.EQUAL, t.getID());

			    	// PreparedQuery contains the methods for fetching query results
			    	// from the datastore
				  PreparedQuery pqu = datastore.prepare(qu);
			    	
				  t.setPolls(pqu.countEntities(FetchOptions.Builder.withOffset(0)) );

				  results.addAnswer(t);
	    	}

			
		} catch (EntityNotFoundException e) {
			return null;
		} 
		
		return results;
	}
	
	public static List<QuestionSession> getQuestionSessions(){
		
    	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    	
    	List<QuestionSession> c = new ArrayList<QuestionSession>();
    	
    	// The Query interface assembles a query
    	Query q = new Query("QuestionSession");

    	// PreparedQuery contains the methods for fetching query results
    	// from the data store
    	PreparedQuery pq = datastore.prepare(q);
    	q.addSort("time", SortDirection.DESCENDING);
    	
    	QuestionSession temp;
    	
    	for (Entity result : pq.asIterable(FetchOptions.Builder.withLimit(10))) {
    		temp = new QuestionSession();
    		temp.setID(result.getKey().getId());
    		temp.setKeyword(result.getProperty("keyword").toString());
    		temp.setPassword(result.getProperty("password").toString());
    		
    		c.add(temp);
    	}
    	return c;
	}
	
	public static List<QuestionSession> getQuestionSessions(String keyword){
		
    	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    	
    	List<QuestionSession> c = new ArrayList<QuestionSession>();
    	
    	// The Query interface assembles a query
    	Query q = new Query("QuestionSession");
    	q.addFilter("questionSessionID", Query.FilterOperator.EQUAL, keyword);
    	q.addSort("time", SortDirection.DESCENDING);

    	// PreparedQuery contains the methods for fetching query results
    	// from the data store
    	PreparedQuery pq = datastore.prepare(q);
    	
    	QuestionSession temp;
    	
    	for (Entity result : pq.asIterable()) {
    		temp = new QuestionSession();
    		temp.setID(result.getKey().getId());
    		temp.setKeyword(keyword);
    		temp.setPassword(result.getProperty("password").toString());
    		
    		c.add(temp);
    	}
    	return c;
	}
	
	public static List<Question> getQuestions(long questionSessionID){
		
    	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    	
    	List<Question> c = new ArrayList<Question>();
    	
    	// The Query interface assembles a query
    	Query q = new Query("Question");
    	q.addFilter("questionSessionID", Query.FilterOperator.EQUAL, questionSessionID);

    	// PreparedQuery contains the methods for fetching query results
    	// from the data store
    	PreparedQuery pq = datastore.prepare(q);
    	
    	Question temp;
    	
    	for (Entity result : pq.asIterable()) {
    		temp = new Question();
    		temp.setID(result.getKey().getId());
    		temp.setQuestionSessionID(questionSessionID);
    		temp.setQuestionText(result.getProperty("question").toString());
    		
    		c.add(temp);
    	}
    	return c;
	}
	
	public static List<Answer> getAnswers(long questionID){
		
    	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    	
    	List<Answer> c = new ArrayList<Answer>();
    	
    	// The Query interface assembles a query
    	Query q = new Query("Answer");
    	q.addFilter("questionID", Query.FilterOperator.EQUAL, questionID);

    	// PreparedQuery contains the methods for fetching query results
    	// from the data store
    	PreparedQuery pq = datastore.prepare(q);
    	
    	Answer temp;
    	
    	for (Entity result : pq.asIterable()) {
    		temp = new Answer();
    		temp.setID(result.getKey().getId());
    		temp.setAnswerText(result.getProperty("answer").toString());
    		
    		c.add(temp);
    	}
    	return c;
	}
	*/
}